<?php

namespace App\Model\form;

use App\Constants\ConstCode;
use App\Model\WeworkAccount;

trait WeworkAccountTrait
{
    /**
     * 获取账号详情
     * @param  $where
     * @return array
     */
    public function getWeworkAccount($where): array
    {
        $result = WeworkAccount::where($where)->first();
        return !empty($result) ? $result->toArray() : [];
    }

    /**
     * 获取账号详情
     * @param  $where
     */
    public function getWeworkAccountInfo($where)
    {
        return WeworkAccount::where($where)->first();
    }


    /**
     * 获取账号列表
     * @param  $where
     * @return array
     */
    public function getWeworkAccountList($where): array
    {
        return WeworkAccount::where($where)->get()->toArray();
    }

    /**
     * 获取账号MarketUserId列表
     * @param  $where
     * @return array
     */
    public function getWeworkAccountIdArr($where): array
    {
        return  WeworkAccount::where($where)->pluck('id')->toArray();
    }

    /**
     * 获取账号MarketUserId
     * @param  $where
     * @return string
     */
    public function getWeworkAccountMarketUserId($where): string
    {
        return  WeworkAccount::where($where)->value('marketUserId');
    }


    /**
     * 编辑更新数据
     * @param  $where
     */
    public function upOrCreatWeworkAccount($where, $params)
    {
        if (!empty($params['real_name']))
            $data['real_name'] = $params['real_name'];

        if (!empty($params['alias_name']))
            $data['alias_name'] = $params['alias_name'];

        if (!empty($params['company']))
            $data['company_name'] = $params['company'];

        if (!empty($params['sex']))
            $data['sex'] = $params['sex'];

        if (!empty($params['name']))
            $data['full_name'] = $params['name'];

        if (!empty($params['avatar']))
            $data['avatar'] = $params['avatar'];

        if (!empty($params['wechat']))
            $data['wechat'] = $params['wechat'];

        if (isset($params['is_login']))
            $data['is_login'] = $params['is_login'];

        if (isset($params['mobile']))
            $data['mobile'] = $params['mobile'];

        if (isset($params['marketUserId']))
            $data['marketUserId'] = $params['marketUserId'];

        if (isset($params['subject_id']))
            $data['subject_id'] = $params['subject_id'];

        return !empty($data) ? WeworkAccount::updateOrCreate($where, $data) : [];
    }

}